﻿@using Apps.Web.Core;
@using Apps.Common;
@using Apps.Models;
@using Apps.Models.Sys;
@using Apps.Locale;
@{
    Layout = "~/Views/Shared/_Index_Layout.cshtml";

    List<permModel> perm = null;
    //化产项目
}
<div style="position: absolute; width: 350px; min-height: 33px; padding-top: 3px;">
    <input id="txtQuery" type="text" class="searchText" style="width:200px" placeholder="可根据化产项目名称查询" />
    @Html.ToolButton("btnQuery", "fa fa-search", Resource.Query, ref perm, "Query", true)
</div>
<div class="mvctool" style="margin-left: 290px;">
    <input id="beginTime" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" type="text" class="searchText" data-options="required:true" style="width:100px" placeholder="填报日期开始" />
    <input id="endTime" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" type="text" class="searchText" data-options="required:true" style="width:100px" placeholder="填报日期截止" />
    <select id="formState" name="formState" style="width:130px" autocomplete="off">
        <option value="">审核状态</option>
        <option value="未审核">未审核</option>
        <option value="审核通过">审核通过</option>
        <option value="审核未通过">审核未通过</option>
    </select>
    @Html.ToolButton("btnQuery2", "fa fa-search", Resource.Query, ref perm, "Query", true)
    @Html.ToolButton("btnCreate", "fa fa-plus", "填报", ref perm, "Create", true)
    @Html.ToolButton("btnEdit", "fa fa-pencil", Resource.Edit, ref perm, "Edit", true)
    @Html.ToolButton("btnExamine", "fa fa-check", "审核", ref perm, "Examine", true)
    @Html.ToolButton("btnDel", "fa fa-trash", Resource.Delete, ref perm, "Delete", true)
    @Html.ToolButton("btnExport", "fa fa-level-up", Resource.Export, ref perm, "Export", true)

</div>
<div id="modalwindow" class="easyui-window" style="width:800px; height:400px;" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
<table>
    <tr>
        <td style="vertical-align: top; width: 200px;padding-top:1px">
            <!--化产项-->
            <table id="projectList" class="easyui-panel" style="width:205px;padding:10px;overflow:auto"></table>
        </td>
        <td style="width:3px;">&nbsp;</td>
        <td style="vertical-align: top">
            <table id="List"></table>
            <div id="pp" class="easyui-pagination" style="background:#efefef;border:1px solid #ccc;"
                 data-options="total:2000,pageSize:10">
            </div>
        </td>
    </tr>
</table>

<div id="Pager"></div>
<input type="hidden" id="itemId" value="" />
@Html.Partial("~/Views/Shared/_Partial_AutoGrid.cshtml")
<script type="text/javascript">
    var selectIndex = -1;
    function getCols(itemId) {
        var columns = [];
        $.ajax({
            url: '/HuaChanProject/GetFields?projectId=' + itemId,
            type: 'GET',
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data != null) {
                        $.each(data, function (i, item) {
                            columns.push({ 'field': item.ID.toString().toUpperCase(), 'title': item.FIELDNAME,'align':'center','width':90, formatter: valueFormat });
                        });
                }
            }
        });
        return columns;
    }
    function valueFormat(val, row, index) {
        var formatValue = val;
        var fieldValues = row.fieldList;
        if (fieldValues != null && fieldValues != undefined && fieldValues.length>0) {
            for (var i = 0; i < fieldValues.length; i++) {
                if (this.field == fieldValues[i].ExtendFieldNAME) {
                    if (fieldValues[i].CONTROL_TYPE == 2) {
                        if (val > fieldValues[i].MAXVALUE) {
                            formatValue = '<span style="color:red; font-weight:bold;">↑' + val + '</span>';
                        } else if (val < fieldValues[i].MINVALUE) {
                            formatValue = '<span style="color:red; font-weight:bold;">↓' + val + '</span>';
                        }
                        else {
                            formatValue = '<span style="color:green; font-weight:bold;">' + val + '</span>';
                        }
                    }
                }
            }
        }
        //$.ajax({
        //    url: '/HC_FORM/GetFormFiledValue?formId=' + row.ID + '&field=' + this.field,
        //    type: 'GET',
        //    async: false,
        //    dataType: 'json',
        //    success: function (data) {
        //        if (data != null) {
        //            if (data.CONTROL_TYPE == 2) {
        //                if (val > data.MAXVALUE) {
        //                    formatValue = '<span style="color:red; font-weight:bold;">↑' + val + '</span>';
        //                } else if (val < data.MINVALUE) {
        //                    formatValue = '<span style="color:red; font-weight:bold;">↓' + val + '</span>';
        //                }
        //                else {
        //                    formatValue = '<span style="color:green; font-weight:bold;">' + val + '</span>';
        //                }
        //            }             }
        //    }
        //});
            return formatValue;
    }

    $(function () {



        var firstId;
        var colss;
        var cols = [];
        $('#projectList').datagrid({
            url: '@Url.Action("GetItemList")',
            methord: 'post',
            width: SetGridWidthSub(220),
            height: SetGridHeightSub(45),
            fitColumns: true,
            sortName: 'ADDTIME',
            sortOrder: 'DESC',
            idField: 'ID',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: false,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            rownumbers: true,//行号
            title: '化产项目列表',
            onLoadSuccess: function (data) {

            },
            columns: [[
                { field: 'ID', title: '', width: 20, hidden: true },
                { field: 'NAME', title: '化产项目', width: 80 },
            ]],
            onClickRow: function (index, data) {
                var row = $('#projectList').datagrid('getSelected');
                if (row != null) {
                    $("#itemId").val(row.ID);

                    curName = row.NAME;
                    curItemId = row.ID;
                    getData(curItemId,1,50,'','','');
                    $("#List").datagrid("clearSelections"); //全部不选中
                    $("#List").datagrid("clearChecked"); //全部不选中
                    $("#List").datagrid("unselectAll"); //全部不选中
                }
            }
        }).datagrid('getPager');
    });

    function getData(projectId, pageIndex, pageSize, examine, beginTime, endTime) {
        $.ajax({
            url: '/HuaChanProject/GetFields',
            type: 'POST',
            data: {
                projectId: projectId,
                pageIndex: pageIndex,
                pageSize: pageSize,
                examine: examine,
                beginTime: beginTime,
                endTime: endTime
            },
            async: false,
            dataType: 'json',
            success: function (data) {
                if (data != null) {
                    $("#List").datagrid({
                        width: SetGridWidthSub(220),
                        height: SetGridHeightSub(76),
                        singleSelect: true,
                        pagination: false,
                        pageNumber: 1,
                        fitColumns: false,
                        sortName: 'ADDTIME',
                        sortOrder: 'desc',
                        idField: 'ID',
                        striped: true, //奇偶行是否区分
                        singleSelect: true,//单选模式
                        //rownumbers: true,
                        selectOnCheck: false,
                        checkOnSelect: false,
                        checkbox: true,
                        frozenColumns: [[]],
                        onLoadSuccess: function (data) {
                            $("#List").datagrid("clearSelections"); //全部不选中
                        },
                        columns: data.columns,
                    })
                    $('#pp').pagination({
                        total: data.total,
                        pageList: [ 50],
                        pageSize: 50,
                        showRefresh: true,
                        onSelectPage: function (pageNumber, pageSize1) {
                            $(this).pagination('loading');
                            var beginTime1 = $("#beginTime").val();
                            var endTime1 = $("#endTime").val();
                            var formState1 = $("#formState").val();
                            getData(projectId, pageNumber, pageSize1, formState1, beginTime1, endTime1)
                            $(this).pagination('loaded');
                        }
                    });
                    //因为有分页，所以数据单独绑定的
                    var rows = { total: data.total, rows: data.list };
                    $("#List").datagrid('loadData', rows);

                }
            }
        });
    }

</script>

@*operation*@
<script type="text/javascript">
    //ifram 返回
    function frameReturnByClose() {
        $("#modalwindow").window('close');
    }
    function frameReturnByReload(flag) {
        var pageInfo = $('#pp').pagination('options');
        var beginTime = $("#beginTime").val();
        var endTime = $("#endTime").val();
        var formState = $("#formState").val();
        var itemId = $("#itemId").val();
        getData(itemId, pageInfo.pageNumber, pageInfo.pageSize, formState, beginTime, endTime);
        $("#List").datagrid("clearSelections"); //全部不选中
        $("#List").datagrid("clearChecked"); //全部不选中
        $("#List").datagrid("unselectAll"); //全部不选中
    }
    function frameReturnByMes(mes) {
        $.messageBox5s(Lang.Tip, mes);
    }
    function showReason(formId) {
        $.get('@Url.Action("GetExamineReason")?formId=' + formId, function (result) {
            $.messager.show({
                title: '审核说明', msg: result, showType: 'show'
            });
        });
    }

    $(function () {
        $("#btnCreate").click(function () {
            var row = $('#projectList').datagrid('getSelected');
            if (row != null) {
                $("#modalwindow").html("<iframe width='100%' height='100%' scrolling='auto' frameborder='0'' src='@Url.Action("Create")?projectId=" + row.ID + "'></iframe>");
                $("#modalwindow").window({ title: Lang.Create, width: 650, height: 500, iconCls: 'fa fa-plus' }).window('open');
            }
            else {
                $.messager.show({
                    title: '提示', msg: '请选择左侧化产项目', showType: 'show'
                });
            }
        });

        $("#btnEdit").click(function () {
            var row = $('#projectList').datagrid('getSelected');
            if (row == null) {
                $.messager.show({
                    title: '提示', msg: '请选择左侧化产项目', showType: 'show'
                });
                return;
            }
            var formRow = $('#List').datagrid('getSelected');
            if (formRow != null)
            {
                if (formRow.EXAMINE == "已审核") { $.messageBox5s(Lang.Tip, '该记录已审核通过，不能再修改'); return; }
                else {
                    $("#modalwindow").html("<iframe width='100%' height='99%'  frameborder='0' src='@Url.Action("Edit")?projectId=" + $("#itemId").val() + "&dataId=" + formRow.ID + "'></iframe>");
                    $("#modalwindow").window({ title: '@Resource.Edit', width: 650, height: 500, iconCls: 'fa fa-pencil' }).window('open');
                }
            }
            else
            {
                $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords');
            }
        });

        $("#btnExamine").click(function () {
            var row = $('#projectList').datagrid('getSelected');
            if (row == null) {
                $.messager.show({
                    title: '提示', msg: '请选择左侧化产项目', showType: 'show'
                });
                return;
            }
            var formRow = $('#List').datagrid('getChecked');
            if (formRow != null && formRow.length > 0) {
                if (formRow.length > 10) {
                    { $.messageBox5s(Lang.Tip, '一次最多审核10条记录'); }
                    return;
                }
                 var formIds = ""
                $.each(formRow, function (i, row) {
                    if (row.EXAMINE !="已审核") {
                       if (formIds == "")
                        {
                             formIds = row.ID;
                        }
                        else {
                             formIds = formIds + "," + row.ID;
                        }
                    }
                });

                if (formIds == "") {
                    $.messageBox5s(Lang.Tip, '没有可审批记录');
                    return;
                }
                else {

                    $("#modalwindow").html("<iframe width='100%' height='99%'  frameborder='0' src='@Url.Action("Examine")?projectId=" + $("#itemId").val() + "&dataIds=" + formIds + "'></iframe>");
                    $("#modalwindow").window({ title: '报单审核', width: 400, height: 280, iconCls: 'fa fa-pencil' }).window('open');
                }
            }
            else
                {$.messageBox5s(Lang.Tip, '请至少选择一条记录');}
        });

        $("#btnQuery").click(function () {
            var queryStr = $("#txtQuery").val();
            if (queryStr == null) {
                queryStr = "";
            }
            $("#projectList").datagrid("load", { queryStr: queryStr });
        });

        $("#btnQuery2").click(function () {
            var row = $('#projectList').datagrid('getSelected');
            if (row == null) {
                $.messager.show({
                    title: '提示', msg: '请选择左侧化产项目', showType: 'show'
                });
                return;
            }
            var formState = $("#formState").val();
            var itemId = $("#itemId").val();
            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();
            getData(itemId, 1, 50, formState,beginTime, endTime);
        });

        $("#btnDel").click(function () {
            var pageInfo = $('#pp').pagination('options');
            var formState = $("#formState").val();
            var itemId = $("#itemId").val();
            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();
            var row = $('#projectList').datagrid('getSelected');
            if (row == null) {
                $.messager.show({
                    title: '提示', msg: '请选择左侧化产项目', showType: 'show'
                });
                return;
            }

            var row = $('#List').datagrid('getSelected');
            if (row != null) {
                $.messager.confirm(index_lang_tip, '@Resource.YouWantToDeleteTheSelectedRecords', function (r) {
                    if (r) {
                        $.post("@Url.Action("Delete")?dataId=" + row.ID, function (data) {
                            getData(itemId, pageInfo.pageNumber, pageInfo.pageSize, formState, beginTime, endTime);
                            $.messageBox5s(Lang.Tip, data.message);
                        }, "json");
                    }
                });
            } else { $.messageBox5s(Lang.Tip, '@Resource.PlaseChooseToOperatingRecords'); }
        });

        $("#btnExport").click(function () {
            var row = $('#projectList').datagrid('getSelected');
            if (row == null) {
                $.messager.show({
                    title: '提示', msg: '请选择左侧化产项目', showType: 'show'
                });
                return;
            }

            var projectId = $("#itemId").val();
            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();
            var formState = $("#formState").val();
            $.post("@Url.Action("CheckExportData")?projectId=" + projectId + "&beginTime=" + beginTime + "&endtime=" + endTime + "&examine=" + formState, function (data) {
                if (data.type == 1)
                {
                    window.location = "@Url.Action("Export")?projectId=" + projectId + "&beginTime=" + beginTime + "&endtime=" + endTime + "&examine=" + formState;
                }
                else
                {
                $.messageBox5s(Lang.Tip, data.message);
                }
            }, "json");
        });
        $(window).resize(function () {
            $('#List').datagrid('resize', {

            }).datagrid('resize', {
                width: SetGridWidthSub(220),
                height: SetGridHeightSub(46)
            });

            $("#p").height(SetGridHeightSub(100));
        });
    });
</script>